<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 日报组件 -->
    <el-scrollbar>
      <daily-work ref="dailyWork" v-for="(dailyWork,index) in workList" :dailyWork="dailyWork" :index="index"
        @setSelectIndex="setSelectIndex" @submit="submitDailyWork"></daily-work>
    </el-scrollbar>
  </div>
</template>

<script>
  import {
    listWork,
    getWork,
    delWork,
    addWork,
    updateWork,
    exportWork
  } from "@/api/system/work";
  import {
    listUser
  } from "@/api/system/user";
  import DailyWork from "@/components/DailyWork/index"
  export default {
    name: "Work",
    components: {
      DailyWork
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 日报表格数据
        workList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          uid: null,
          todayCompleteContent: null,
          tomorrowPlan: null,
          problems: null,
          isDel: null,
          nickName:''
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          uid: [{
            required: true,
            message: '请选择',
            trigger: 'change'
          }],
          todayCompleteContent: [{
              required: true,
              message: '请输入内容',
              trigger: 'blur'
            },
            {
              min: 10,
              message: '长度在 10 字符以上',
              trigger: 'blur'
            }
          ],
          tomorrowPlan: [{
              required: true,
              message: '请输入内容',
              trigger: 'blur'
            },
            {
              min: 10,
              message: '长度在 10 字符以上',
              trigger: 'blur'
            }
          ],
          problems: [{
              required: true,
              message: '请输入内容',
              trigger: 'blur'
            },
            {
              min: 5,
              message: '长度在 5 字符以上',
              trigger: 'blur'
            }
          ],

        },
        selectIndex: null,
        //定时器
        interval: null
      };
    },
    created() {
      this.getList();
      this.interval = setInterval(this.autoSave, 30000);
    },
    methods: {
      setSelectIndex(index) {
        //如果切换日报
        if (this.selectIndex != null && this.selectIndex != index) {
          this.submitDailyWork(this.workList[this.selectIndex])
        }
        this.selectIndex = index
      },
      clickDiv() {
        // this.getFocus();
      },
      onReachBottom() {
        console.log('onReachBottom')
      },
      submitDailyWork(dailyWork) {
        this.$alert('日报未保存，是否提交日报修改？', '日报编辑', {
          confirmButtonText: '确定',
          callback: action => {
            if (action == 'confirm') {
              if (dailyWork.id != null) {
                updateWork(dailyWork).then(response => {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                });
              } else {
                addWork(dailyWork).then(response => {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                });
              }
            }
            //提交过后将选择置空
            this.selectIndex = null;
          }
        });
      },
      onSelectionChange(dailyWork) {
      },
      doubleClick(id) {
        this.handleUpdate({
          id: id
        });
      },
      autoSave() {
        let workList = this.workList;
        for (let i = 0; i < workList.length; i++) {
          let dailyWork = workList[i]
          if (dailyWork.focus == true) {
            if (dailyWork.id != null) {
              updateWork(dailyWork).then(response => {
                this.msgSuccess("自动保存成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWork(dailyWork).then(response => {
                this.msgSuccess("自动修改成功");
                this.open = false;
                this.getList();
              });
            }
          }
        }
      },
      /** 查询日报列表 */
      getList() {
        this.loading = true;
        listWork(this.queryParams).then(response => {
          this.workList = response.rows;
          for (var i = 0; i < this.workList.length; i++) {
            let dailyWork = this.workList[i]
            dailyWork.focus = false
            dailyWork.modifytTodayCompleteContent = false
            dailyWork.modifyTomorrowPlan = false
            dailyWork.modifyProblems = false
          }
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          uid: null,
          todayCompleteContent: null,
          nickName: null,
          createTime: null,
          tomorrowPlan: null,
          problems: null,
          isDel: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加日报";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getWork(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改日报";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateWork(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWork(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除日报编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWork(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有日报数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportWork(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
</script>
<style>
  .el-scrollbar {
    height: 100%;
  }

  .el-scrollbar_wrap {
    overflow: auto;
    width: 100%;
    height: 100%;
  }

  .app-container {
    height: 1000px;
    overflow-y: hidden;
  }
</style>
